package com.xyh.controller;

import com.xyh.service.YxcLoginService;

import com.xyh.service.YxcUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
public class YxcLoginController {

    @Resource
    private YxcLoginService yxcLoginService;
    @Resource
    private YxcUserService yxcUserService;

    @RequestMapping("/check_login")
    public String checkLogin(HttpServletRequest request, RedirectAttributes attributes){
        String login_telephone = request.getParameter("login_telephone");
        String login_password = request.getParameter("login_password");

        String password = yxcLoginService.selectPwdByPhone(login_telephone);
        if (login_telephone.length() != 11) return "redirect:to_yxc_login";
        else {
            if (password == null) {
                yxcLoginService.insertNewLogin(login_telephone, login_password);
                yxcUserService.insertNewUser(login_telephone, login_password);
            } else if (!password.equals(login_password)) return "redirect:to_yxc_login";
        }
        attributes.addAttribute("login_telephone", login_telephone);
        attributes.addAttribute("situation",0);
        attributes.addAttribute("current_page", 1);
        attributes.addAttribute("page_size",1);
        return "redirect:to_yxc_main";
    }

    @RequestMapping("/to_yxc_login")
    public String toYxcLogin(){
        return "yxc_login";
    }


}
